package com.atguigu.gmall.payment.controller;

import com.atguigu.gmall.common.bean.UserInfo;
import com.atguigu.gmall.common.exception.OrderException;
import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.payment.interceptors.LoginInterceptor;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 86182
 * @Date: 2021/8/29
 * @Time: 18:13
 * @Version:
 */
@Controller
public class PaymentController {
    @Autowired
    public PaymentService paymentService;

    @GetMapping("pay.html")
    public String topay(@RequestParam("orderToken") String orderToken, Model model){
        // 查询订单
        OrderEntity orderEntity = this.paymentService.queryOrderByOrderToken(orderToken);
        if (orderEntity == null){
            throw new OrderException("非法参数");
        }
        // 判断是否是我的订单
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        Long userId = userInfo.getUserId();
        if (orderEntity.getUserId() != userId){
            throw new OrderException("当前订单不属于您");
        }

        //判断订单转台
        if (orderEntity.getStatus() != 0){
            throw new OrderException("当前订单不能支付");
        }

        model.addAttribute("orderEntity",orderEntity);
        return "pay";
    }
}
